#include "homepage.h"
#include "appointmentpage.h"
#include "doctorcommunicationpage.h"
#include "departmentpage.h"
#include "doctorsindepartmentpage.h"
#include "../Bodytest/widget.h"
#include "myappointmentspage.h"  // 添加我的就诊页面的头文件
#include <QHBoxLayout>
#include <QTreeWidget>
#include <QPushButton>
#include <QStackedWidget>
#include <QLabel>
#include <QPixmap>
#include <QVBoxLayout>
#include <QSpacerItem>
#include "../AppointmentRegister/Client/appointment/user_communication.h"
#include "../DoctorList/doctorlist.h"

HomePage::HomePage(int currentID, QString &role, QWidget *parent) : QWidget(parent) {
    this->currentID = currentID;
    this->role = role;

    QHBoxLayout *mainLayout = new QHBoxLayout(this);

    // 创建侧边菜单
    QVBoxLayout *sidebarLayout = new QVBoxLayout();

    // 头像显示部分
    QLabel *avatarLabel = new QLabel(this);
    QPixmap avatarPixmap(":/images/avatar.png");  // 假设你有一个 avatar.png 图片
    avatarLabel->setPixmap(avatarPixmap.scaled(80, 80, Qt::KeepAspectRatio));  // 设置头像大小
    avatarLabel->setAlignment(Qt::AlignCenter);

    sidebarLayout->addWidget(avatarLabel);  // 添加头像到布局
    sidebarLayout->addSpacing(10);  // 添加间隔，避免头像与菜单重叠

    // 创建侧边菜单
    QTreeWidget *sidebarMenu = new QTreeWidget(this);
    sidebarMenu->setHeaderLabel("功能菜单");
    sidebarMenu->setFixedWidth(200);  // 设置侧边菜单宽度
    sidebarMenu->setStyleSheet("QTreeWidget { background-color: #f0f0f0; border: none; }");  // 设置菜单背景

    // 添加功能菜单项
    QTreeWidgetItem *appointment = new QTreeWidgetItem(sidebarMenu, QStringList() << "预约挂号");
    QTreeWidgetItem *doctorCommunication = new QTreeWidgetItem(sidebarMenu, QStringList() << "医生交流");
    QTreeWidgetItem *viewDepartments = new QTreeWidgetItem(sidebarMenu, QStringList() << "查看科室");
//    QTreeWidgetItem *regi = new QTreeWidgetItem(sidebarMenu, QStringList() << "挂号");
    QTreeWidgetItem *myAppointments = new QTreeWidgetItem(sidebarMenu, QStringList() << "我的就诊");  // 新增项
    QTreeWidgetItem *bodyTest = new QTreeWidgetItem(sidebarMenu, QStringList() << "健康评估");  // 新增项
//    QTreeWidgetItem *doctorList = new QTreeWidgetItem(sidebarMenu, QStringList() << "医生列表");  // 新增项

    sidebarLayout->addWidget(sidebarMenu);  // 将侧边菜单添加到布局中

    // 创建内容显示区
    stackedWidget = new QStackedWidget(this);

    // 添加页面
    stackedWidget->addWidget(new AppointmentPage(this->currentID, this->role));  // 挂号页面
    MyAppointmentsPage *myAppoint =  new MyAppointmentsPage(this->currentID,this->role) ;
//    stackedWidget->addWidget(new AppointmentPage(this->currentID, this->role));  // 预约挂号页面
    stackedWidget->addWidget(new user_communication());  // 医生交流页面
    DepartmentPage *departmentPage = new DepartmentPage();  // 查看科室页面
    stackedWidget->addWidget(departmentPage);
    stackedWidget->addWidget(myAppoint);  // 我的就诊页面
    stackedWidget->addWidget(new Widget(this->currentID, this->role));  // 我的就诊页面
    // 连接侧边菜单的信号
    connect(sidebarMenu, &QTreeWidget::itemClicked, this, [this, sidebarMenu](QTreeWidgetItem *item, int) {
        if (item == sidebarMenu->topLevelItem(0)) {
            stackedWidget->setCurrentIndex(0);  // 预约挂号
        } else if (item == sidebarMenu->topLevelItem(1)) {
            stackedWidget->setCurrentIndex(1);  // 医生交流
        } else if (item == sidebarMenu->topLevelItem(2)) {
            stackedWidget->setCurrentIndex(2);  // 查看科室
            emit DepartLoad();
        } else if (item == sidebarMenu->topLevelItem(3)) {
            stackedWidget->setCurrentIndex(3);  // 我的就诊
           emit MyappointLoad(this->currentID);
        } else if (item == sidebarMenu->topLevelItem(4)) {
            stackedWidget->setCurrentIndex(4);  // 健康评估
        }
    });

    // 连接部门页面的信号
    connect(departmentPage, &DepartmentPage::departmentSelected, this, [this](const QString &departmentName) {
        DoctorsInDepartmentPage *doctorsPage = new DoctorsInDepartmentPage(departmentName);
        stackedWidget->addWidget(doctorsPage);  // 动态添加医生页面
        stackedWidget->setCurrentWidget(doctorsPage);  // 切换到医生页面
    });

    connect(this, &HomePage::MyappointLoad, myAppoint, &MyAppointmentsPage::loadMyAppointments);
    connect(this, &HomePage::DepartLoad, departmentPage, &DepartmentPage::loadData);

    // 创建退出登录按钮
    QPushButton *logoutButton = new QPushButton("退出登录", this);
    logoutButton->setStyleSheet("QPushButton { background-color: #ff4d4d; color: white; border-radius: 5px; font-weight: bold; padding: 10px; }"
                                "QPushButton:hover { background-color: #e60000; }");  // 设置按钮样式
    connect(logoutButton, &QPushButton::clicked, this, &HomePage::logout);

    // 设置退出按钮的位置
    QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    sidebarLayout->addItem(spacer);  // 将退出按钮放置在底部

    sidebarLayout->addWidget(logoutButton);  // 将退出按钮添加到布局中

    // 将侧边菜单布局和内容显示区布局添加到主布局
    mainLayout->addLayout(sidebarLayout);
    mainLayout->addWidget(stackedWidget);  // 将堆叠窗口添加到布局中

    setLayout(mainLayout);  // 设置主布局
}

// 退出登录的槽函数
void HomePage::logout() {
    QMessageBox::StandardButton reply;
          reply = QMessageBox::question(this, "退出登录", "确认退出登录吗?",
                                        QMessageBox::Yes | QMessageBox::No);
          if (reply == QMessageBox::Yes) {
              // 清除用户数据（例如清除会话、缓存、设置等）
              // 跳转到登录页面
              emit userLoggedOut();
              // 或者直接关闭当前页面
              this->close();
          }
}
